<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth;
class YanpanController extends Controller
{

    public function mark(Request $request)
    {
        if($request->isMethod('post')){
            $result = Input::except('_token');
            foreach ($result as $k=>$v){
                $where['keys'] = $k;
                $data['val'] = $v;
                $re = DB::table('jifenxiang')->where($where)->update($data);
            }

           
            return ['status'=>1,'msg'=>'保存成功'];
           

        }

        $res =  DB::table('jifenxiang')->get();
        foreach($res as $k=>$v ){
            $config[$v->keys] = $v->val;
        }
        return view('admin.yanpan.mark', ['config' => $config]);
    }








    public function rule(request $request)
    {   

        if($request->rows) {
            $map['yanpan_rule.is_del'] = '0';
            $res = DB::table('yanpan_rule')->where($map)->leftJoin('admin_users as e', 'yanpan_rule.user_id', '=', 'e.id')->orderby('yanpan_rule.id','desc')->select('yanpan_rule.*','e.name as user_id')->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.yanpan.rule');
        }
        // $map['is_del'] = '0';
        // $res = DB::table('yanpan_rule')->where($map)->orderby('id','desc')->paginate(10);
        // $count = DB::table('yanpan_rule')->where($map)->count();

        // return view('admin.yanpan.rule', ['res' => $res,'count'=>$count]);
    }

    public function add_rule(Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('yanpan_rule')->insert($data);
            
             if ($re) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }

        }else{
             return view('admin.yanpan.add_rule');
        }
       
    }

    public function edit_rule($id,Request $request)
    {
        $re = DB::table('yanpan_rule')->where('id',$id)->first();

        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('yanpan_rule')->where('id',$id)->update($data); 

            if ($re) {
                 return ['status'=>1,'msg'=>'修改成功'];
            }else{
                 return ['status'=>0,'msg'=>'修改失败'];
            }        
        }
     
        return view('admin.yanpan.edit_rule', ['re' => $re,'id'=>$id]);

       
    }

    public function del_rule($id)
    {
        $re = db::table('yanpan_rule')->where('id',$id)->delete();


        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
        return redirect()->back();
    }

    public function color(request $request)
    {   

        if($request->rows) {
            $map['color.is_del'] = '0';
            $res = DB::table('color')->where($map)->leftJoin('admin_users as e', 'color.user_id', '=', 'e.id')->orderby('color.id','desc')->select('color.*','e.name as user_id')->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.yanpan.color');
        }

        // $map['is_del'] = '0';
        // $res = DB::table('color')->where($map)->orderby('id','desc')->paginate(10);
        // $count = DB::table('color')->where($map)->count();

        // return view('admin.yanpan.color', ['res' => $res,'count'=>$count]);
    }

    public function add_color(Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('color')->insert($data);
   
             if ($re) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }

        }else{
             //研判规则
            $rule = db::table('yanpan_rule')->get();
            return view('admin.yanpan.add_color',['rule'=>$rule]);
        }
        
      
    }

    public function edit_color($id,Request $request)
    {
        $re = DB::table('color')->where('id',$id)->first();

        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('color')->where('id',$id)->update($data);

            if ($re) {
                 return ['status'=>1,'msg'=>'修改成功'];
            }else{
                 return ['status'=>0,'msg'=>'修改失败'];
            }

          
        }
        //研判规则
        $rule = db::table('yanpan_rule')->get();
        return view('admin.yanpan.edit_color', ['re' => $re,'rule'=>$rule,'id'=>$id]);
    }

    public function del_color($id)
    {
        $re = db::table('color')->where('id',$id)->delete();



        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }

        return redirect()->back();
    }

    public function fense(request $request)
    {
        

        if($request->rows) {


            if($request->color_id){
                $map['color_id'] = input::get('color_id');
            }
            if($request->name){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if($request->area_id){
                $map['area_id'] = input::get('org_id');
            }
            if($request->input('jibie') != null){
                $map['is_imp'] = $request->jibie;
            }


            $map['units.is_del'] = '0';
            $res = DB::table('units')->where($map)->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')->orderby('units.id','desc')->select('units.*','e.name as user_id')->paginate($request->rows);
            $list = $res->items();

             foreach ($list as $k=>$v){
                 $list[$k]->type = getValue($v->type,'unit_type' ,'id' ,'name' );
                 if($v->is_imp == 1){
                     $list[$k]->is_imp = '省级';
                 }elseif($v->is_imp == 2){
                     $list[$k]->is_imp = '市级';
                 }elseif($v->is_imp == 3){
                     $list[$k]->is_imp = '县级';
                 }elseif($v->is_imp == 4){
                     $list[$k]->is_imp = '乡级';
                 }else{
                     $list[$k]->is_imp = '非重点';
                 }
            }


            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            //颜色
            $color = db::table('color')->get();
            return view('admin.yanpan.fense',['color'=>$color]);
        }



        // $map['is_del'] = '0';
        // $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        // $count = DB::table('units')->where($map)->count();

        // //颜色
        // $color = db::table('color')->get();
        // return view('admin.yanpan.fense', ['res' => $res,'count'=>$count,'color'=>$color]);
    }

    public function view($id)
    {
        $re = DB::table('units')->where('id',$id)->first();
        return view('admin.yanpan.view', ['re' => $re]);
    }

    public function save_color($id,Request $request)
    {
        $re = DB::table('units')->where('id',$id)->first();

        if($request->isMethod('post')){
            $input = $request->all();
            $data = Input::except('_token');
            $data['color_id'] = $input['color_id'];

            $re = DB::table('units')->where('id',$id)->update($data);
            
            if ($re) {
                 return ['status'=>1,'msg'=>'修改成功'];
            }else{
                 return ['status'=>0,'msg'=>'修改失败'];
            }

        }
        //研判规则
        $color = db::table('color')->get();
        return view('admin.yanpan.save_color', ['re' => $re,'color'=>$color,'id'=>$id]);
    }





    public function dw_jiankong(request $request)
    {
        return view('admin.yanpan.dw_jiankong');
    }

    public function get_units(request $request)
    {
        $info = db::table('units')->get();

        $lastinfo = array();
        foreach ($info as $key => $value) {
            $url = "<br><a href='".url('admin/units/view',$value->id)."' width='100%'style='color: red'>点击查看</a>";
            $laststr = '公司名：'.$value->name.$url;
            $lastinfo[$key][0] = $value->jd;
            $lastinfo[$key][1] = $value->wd;
            $lastinfo[$key][2] = $laststr;
        }
        return $lastinfo;
    }

    public function zdry_jiankong(Request $request)
    {

          if($request->rows) {


            if($request->name){
                $map[] = array('zhongdianrenyuan.name','like','%'.input::get('name').'%');
            }
            if($request->unit_id){
                $map['zhongdianrenyuan.unit_id'] = input::get('unit_id');
            }
         


            $map['zhongdianrenyuan.is_del'] = '0';
            $res = DB::table('zhongdianrenyuan')->where($map)->
            leftJoin('admin_users as e', 'zhongdianrenyuan.user_id', '=', 'e.id')
            ->
             leftJoin('units as u','zhongdianrenyuan.unit_id', '=', 'u.id')
            ->
            orderby('zhongdianrenyuan.id','desc')
            ->
            select('zhongdianrenyuan.*','e.name as user_id','u.name as uname')->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
             //单位
            $units = db::table('units')->where('is_del','0')->get();
            return view('admin.yanpan.zdry_jiankong', ['units'=>$units]);
        }




        // $map['is_del'] = '0';
        // $res = DB::table('zhongdianrenyuan')->where($map)->paginate(10);
        // $count = DB::table('zhongdianrenyuan')->where($map)->count();
        // //单位
        // $units = db::table('units')->where('is_del','0')->get();
        // return view('admin.yanpan.zdry_jiankong', ['res' => $res,'count'=>$count,'units'=>$units]);
    }

    public function view_zdry($id)
    {
        $re = DB::table('zhongdianrenyuan')->where('id',$id)->first();
        return view('admin.yanpan.view_zdry', ['re' => $re]);
    }

    public function qiyexinyong()
    {
        return view('admin.yanpan.qiyexinyong');
    }

    public function jingzong()
    {
        return view('admin.yanpan.jingzong');
    }

    public function kaoping()
    {
        return view('admin.yanpan.kaoping');
    }

    public function youliang()
    {
        return view('admin.yanpan.youliang');
    }

    public function fumian()
    {
        return view('admin.yanpan.fumian');
    }

    public function heimingdan()
    {
        return view('admin.yanpan.heimingdan');
    }

    public function jiejing()
    {
        return view('admin.yanpan.jiejing');
    }
    
    public function yuqing()
    {
        return view('admin.yanpan.yuqing');
    }
    
    public function anjian()
    {
        return view('admin.yanpan.anjian');
    }

    public function fx_pingjia()
    {
        return view('admin.yanpan.fx_pingjia');
    }

    public function fengxian()
    {
        return view('admin.yanpan.fengxian');
    }
    	public function add_fxdpg()
    {
        return view('admin.yanpan.add_fxdpg');
    }
    	public function edit_fxdpg()
    {
        return view('admin.yanpan.edit_fxdpg');
    }
    	public function edit_jiejing()
    {
        return view('admin.yanpan.edit_jiejing');
    }
    	public function add_yuqing()
    {
        return view('admin.yanpan.add_yuqing');
    }
    	public function edit_yuqing()
    {
        return view('admin.yanpan.edit_yuqing');
    }
    	public function add_anjian()
    {
        return view('admin.yanpan.add_anjian');
    }
    	public function edit_anjian()
    {
        return view('admin.yanpan.edit_anjian');
    }
	
	public function add()
    {
        return view('admin.yanpan.add');
    }
	public function jibenqingkuang()
    {
        return view('admin.yanpan.jibenqingkuang');
    }
	public function add2()
    {
        return view('admin.yanpan.add2');
    }
	public function add3()
    {
        return view('admin.yanpan.add3');
    }
	public function add4()
    {
        return view('admin.yanpan.add4');
    }
	public function kaoping2()
    {
        return view('admin.yanpan.kaoping2');
    }
	public function addkp()
    {
        return view('admin.yanpan.addkp');
    }
}
